package com.weblog.article.dao;

import com.weblog.model.article.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.NoRepositoryBean;

import java.util.List;

/**
 * category数据访问接口
 * @author Administrator
 *
 */
public interface CategoryDao extends JpaRepository<Category,String>, JpaSpecificationExecutor<Category> {
    /**
     * 根据分类名查询分类
     * @param cname
     * @return
     */
    Category findCategoryByCname(String cname);

    List<Category> findByState(String state);

    /**
     * 更改为父类目
     */
    @Query(value = "update tb_category set isparent =?1 where id=?2",nativeQuery = true)
    @Modifying
    void updateIsparent(String isparent, String id);

    /**
     * state更改为0|1，逻辑删除|恢复。根据id修改
     */
    @Query(value = "update tb_category set state =?1 where id=?2",nativeQuery = true)
    @Modifying
    void updateStateById(String state, String id);

    /**
     * 通过父节点查询分类集合，即指定父id的分类集合
     * @param parentid
     * @return
     */
    List<Category> findCategoriesByParentId(String parentid);


    /**
     * 根据id查询名字
     * @param id
     * @return
     */
    @Query(value = "select cname from tb_category where id =?1",nativeQuery = true)
    String findCnameById(String id);

    /**
     * 根据名字查询id
     * @param cname
     * @return
     */
    @Query(value = "select id from tb_category where cname =?1",nativeQuery = true)
    String findIdByCname(String cname);

    /**
     * 根据id查询登记
     * @param id
     * @return
     */
    @Query(value = "select clevel from tb_category where id=?1",nativeQuery = true)
    int findlevelById(String id);


    /**
     * 根据分类等级查询分类集合
     * @param level
     * @return
     */
    List<Category> findCategoriesByClevel(int level);

    /**
     * 根据id查询分类等级
     * @param cid
     * @return
     */
    @Query(value = "select clevel from tb_category where id=?1",nativeQuery = true)
    int findClevelById(String cid);
}
